package com.sky.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.xiaoymin.knife4j.core.util.StrUtil;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StreamUtils;

import java.util.List;
import java.util.Objects;

/**
 * @author : [caohailong]
 * @version : [v1.0]
 * @Description : 菜品管理
 * @date : [2023/12/11 15:11]
 */
@Service
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 新增菜品
     *
     * @param dishDTO 菜品信息
     */
    @Override
    public void saveWithFlavor(DishDTO dishDTO) {

        Dish dish = Dish.builder().build();
        BeanUtils.copyProperties(dishDTO, dish);
        //新增菜品
        Long dishId = dishMapper.insetDish(dish);
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (!CollectionUtils.isEmpty(flavors)){
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dishId);
            });
            //向口味表插入n条数据
            dishFlavorMapper.insertBatch(flavors);//后绪步骤实现
        }
    }

    /**
     * 菜品分页查询
     *
     * @param dishPageQueryDTO 查询请求信息
     * @return 菜品信息
     */
    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        //使用分页查询
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        // 进行查询
        List<DishVO> dishList = dishMapper.getDishPageList(dishPageQueryDTO);
        // 封装到PageInfo，PageInfo会自动获取分页信息
        PageInfo<DishVO> pageInfo = new PageInfo<>(dishList);
        long total = pageInfo.getTotal();

        return new PageResult(total, pageInfo.getList());
    }

    /**
     * 菜品删除
     *
     * @param ids 菜品id
     * @return
     */
    @Override
    public Result<?> deleteDishByIds(List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)){
            return Result.error("未查询到需要删除的菜品信息！");
        }
        //判断当前菜品是否能够删除---是否存在起售中的菜品？？
        List<Dish> dishList = dishMapper.getDishByIds(ids);
        dishList.forEach(dish -> {
            if (Objects.equals(dish.getStatus(), StatusConstant.ENABLE)){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        });
        //判断当前菜品是否能够删除---是否被套餐关联了？？
        List<Long> setmealDishIds = setmealDishMapper.getSetmealDishByIds(ids);
        if (!CollectionUtils.isEmpty(setmealDishIds)){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        int delete = dishMapper.deleteDishByIds(ids);
        return Result.success("删除成功！");
    }

    /**
     * 修改菜品禁用状态
     * @param status 菜品状态
     * @param id 菜品id
     * @return 更新结果
     */
    @Override
    public Result<?> updateDishStatusById(Integer status, Long id) {
        dishMapper.updateDishStatusById(status,id);
        return Result.success("修改成功!");
    }

    /**
     * 根据id查询菜品
     *
     * @param id 菜品id
     * @return 菜品信息
     */
    @Override
    public Result<?> getDishById(Long id) {
        DishVO dishVO = dishMapper.getDishById(id);
        return Result.success(dishVO);
    }

    /**
     * 编辑修改菜品信息
     * @param dishDTO 菜品信息
     * @return 操作结果
     */
    @Override
    public Result<?> updateDish(DishDTO dishDTO) {
         Dish dish = new Dish();
         BeanUtils.copyProperties(dishDTO, dish);
         //修改菜品
        dishMapper.updateDish(dish);
        Long dishId = dish.getId();
        //删除原来口味
        dishFlavorMapper.deleteByDishId(dishId);
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (!CollectionUtils.isEmpty(flavors)){
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dishId);
            });
            //向口味表插入n条数据
            dishFlavorMapper.insertBatch(flavors);//后绪步骤实现
        }
        return Result.success("修改成功");
    }

    /**
     * 根据分类id查询菜品
     * @param categoryId 菜品分类id
     * @return 查询信息
     */
    @Override
    public List<Dish> list(Long categoryId) {
        Dish dish = Dish.builder()
                .categoryId(categoryId)
                .status(StatusConstant.ENABLE)
                .build();
        return dishMapper.list(dish);
    }
}
